/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 29.07.2005
 *
 *************************************************/
package fightplan.business;

import java.io.Serializable;
import java.text.Collator;

import fightplan.core.Util;

public class Dojo implements Serializable, Comparable<Dojo> {
	private static final long serialVersionUID = 1L;

	private String name = "";

	private static int counter = 1;

	public Dojo() {
		setName("Dojo " + (counter++));
	}

	// get / set

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Dojo) {
			return Util.equals(this.name, ((Dojo) obj).name);
		}
		return false;
	}

	public int calcPrice(Tournament tournament) {
		int price = getFighterCount(tournament) * tournament.getPriceFighter();
		for (Category category : tournament.getCategories()) {
			if (!category.isTeam())
				continue;
			price += tournament.getPriceTeam() * getTeamCount(category);
		}
		return price;
	}

	public int getPrice(Tournament tournament) {
		return calcPrice(tournament);
	}

	public int getFighterCount(Tournament tournament) {
		int count = 0;
		for (Fighter fighter : tournament.getFighters()) {
			if (this.equals(fighter.getDojo())) {
				for (Category category : tournament.getCategories()) {
					if (category.getFighters().contains(fighter)) {
						count++;
					}
				}
			}
		}
		return count;
	}

	public int getTeamCount(Category category) {
		int count = 0;
		for (AbstractFighter fighter : category.getFighters()) {
			if (this.equals(fighter.getDojo()))
				count++;
		}
		return count;
	}

	public int compareTo(Dojo dojo) {
		String name = dojo != null ? dojo.getName() : "";
		return Collator.getInstance().compare(this.name != null ? this.name : "", name != null ? name : "");
	}

	@Override
	public String toString() {
		return name;
	}

}
